package com.kehutong.payment.service;

import java.util.Map;
import org.coraframework.inject.Inject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.util.Objects;
import com.kehutong.common.errcode.Result;
import com.kehutong.payment.dto.OrderRefundReqDto;
import com.kehutong.payment.enums.PayTypeEnum;
import com.kehutong.payment.errcode.PayErrorCode;
import com.kehutong.payment.errcode.PayResult;
import com.kehutong.payment.vo.Merchant;

@WebService("/payment/wxpay/gateway/service")
public class WxpayGatewayService {

    protected Logger logger = LoggerFactory.getLogger(WxpayGatewayService.class);

    @Inject
    private RouteService routeService;
    @Inject
    private WxpayService wxpayService;
    @Inject
    private WechatRefundServiceImpl wechatRefundService;

    @ReqMapping("/unified/create")
    public Object unifiedCreate(Map<String, String> params){
        params.remove("token");
        logger.info("============WxpayGatewayService.unifiedCreate请求参数：{}================", params);

        String subOrderNo = params.get("subOrderNo");
        String body = params.get("body");
        String spbill_create_ip = params.get("spbill_create_ip");
        String total_fee = params.get("total_fee");
        String notify_url = params.get("notify_url");
        String openid = params.get("openid");
        String sub_openid = params.get("sub_openid");
        String payType = params.get("payType");
        String companyNo = params.get("companyNo");
        String communityNo = params.get("communityNo");
        if (Objects.isEmpty(subOrderNo)
            || Objects.isEmpty(body)
            || Objects.isEmpty(total_fee)
            || Objects.isEmpty(spbill_create_ip)
            || Objects.isEmpty(notify_url)
            || Objects.isEmpty(payType)
            || Objects.isEmpty(companyNo)
            || (!payType.equals(PayTypeEnum.wxpay_web.name()) && (Objects.isEmpty(openid)))
        ) {
            return PayResult.error(PayErrorCode.E215001);
        }

        try {
            params.remove("token");
            params.remove("companyNo");
            params.remove("communityNo");

            if (payType.equals(PayTypeEnum.wxpay_web.name())) {
                params.put("trade_type", "NATIVE");
            } else {
                params.put("trade_type", "JSAPI");
            }
            Merchant merchant = routeService.route(companyNo, communityNo);
            if (Objects.isNull(merchant)) {
                return Result.error(0, "请配置商户！！！");
            }
            
            return wxpayService.unifiedCreate(merchant, params, companyNo, communityNo);
        } catch (Exception e) {
            logger.error("=========WxpayGatewayService.create.exception:", e);
            return Result.error(0, "系统异常");
        }
    }

    @ReqMapping("/v2/unified/create")
    public Object v2UnifiedCreate(Map<String, String> params){
        params.remove("token");
        logger.info("============WxpayGatewayService.v2UnifiedCreate请求参数：{}================", params);

        String subOrderNo = params.get("subOrderNo");
        String body = params.get("body");
        String spbill_create_ip = params.get("spbill_create_ip");
        String total_fee = params.get("total_fee");
        String notify_url = params.get("notify_url");
        String openid = params.get("openid");
        String sub_openid = params.get("sub_openid");
        String payType = params.get("payType");
        String companyNo = params.get("companyNo");
        String communityNo = params.get("communityNo");
        if (Objects.isEmpty(subOrderNo)
            || Objects.isEmpty(body)
            || Objects.isEmpty(total_fee)
            || Objects.isEmpty(spbill_create_ip)
            || Objects.isEmpty(notify_url)
            || Objects.isEmpty(payType)
            || Objects.isEmpty(companyNo)
            || (!payType.equals(PayTypeEnum.wxpay_web.name()) && (Objects.isEmpty(openid)))
        ) {
            return PayResult.error(PayErrorCode.E215001);
        }

        try {
            params.remove("token");
            params.remove("companyNo");
            params.remove("communityNo");

            if (payType.equals(PayTypeEnum.wxpay_web.name())) {
                params.put("trade_type", "NATIVE");
            } else {
                params.put("trade_type", "JSAPI");
            }
            Merchant merchant = routeService.routeV2(companyNo, communityNo);
            if (Objects.isNull(merchant)) {
                return Result.error(0, "请配置商户！！！");
            }

            return wxpayService.v2UnifiedCreate(merchant, params, companyNo, communityNo);
        } catch (Exception e) {
            logger.error("=========WxpayGatewayService.v2UnifiedCreate.exception:", e);
            return Result.error(0, "系统异常");
        }
    }

    @ReqMapping("/unified/query")
    public Object unifiedQuery(Map<String, String> params){
        String companyNo = params.get("companyNo");
        String communityNo = params.get("communityNo");
        if (Objects.isEmpty(companyNo)
        ) {
            return PayResult.error(PayErrorCode.E215001);
        }

        try {
            Merchant merchant = routeService.route(companyNo, communityNo);
        } catch (Exception e) {
            logger.error("=========WxpayGatewayService.unifiedQuery.exception:", e);
            return Result.error(0, "系统异常");
        }
        return null;
    }

    @ReqMapping("/unified/refund")
    public Object refund(OrderRefundReqDto orderRefundReqDto){
        return wechatRefundService.refundOrder(orderRefundReqDto);
    }

}
